

dataFolder= '12_15_17_01';
fileNumberNoDrugs=4; %for a recording called "ch0_fileNumber" goes up to 64
fileNumberGabazine=54;
trials=3;
path1='C:\Users\tomas\Documents\1. Harvard\2017 - Spring\Regehr lab rotation\Data\';
path2=[ dataFolder(1:2) dataFolder(6:8) '\' dataFolder(1:8) '\' dataFolder '\'];


ISteps=[-30:5:30];

lookup=[7,8,9,10,11,12,13,6,5,4,3,2,1]; %%2021--> This is confusing AF, but I got it. The number indicates the index of the corresponding value of current injection in the vector -30:5:30. e.g. step_7 -->lookup(7+1)=6--> ISteps(6)=-5 pA  Therefore step_7 corresnpoinds to -5 pA. fk lol

tracesNoDrugs=NaN(550000,numel(ISteps),trials);
tracesGabazine=NaN(550000,numel(ISteps),trials);


    for i=1:numel(ISteps)
        for j=1:trials;
       Ibw1= IBWread([path1 path2 'ch0_' num2str((fileNumberNoDrugs-1)+i+(13*(j-1))) '.ibw']);
       Ibw2= IBWread([path1 path2 'ch0_' num2str((fileNumberGabazine-1)+i+(13*(j-1))) '.ibw']);
       idxND1=strfind(Ibw1.WaveNotes,'step_');
        idxND2=strfind(Ibw1.WaveNotes,';HC');
         idxG1=strfind(Ibw2.WaveNotes,'step_');
        idxG2=strfind(Ibw2.WaveNotes,';HC');

        tracesNoDrugs(:,lookup(str2num(Ibw1.WaveNotes(idxND1+5:idxND2-1))+1),j)=Ibw1.y;
        tracesGabazine(:,lookup(str2num(Ibw2.WaveNotes(idxG1+5:idxG2-1))+1),j)=Ibw2.y;
        
    end 
    end

stepIdx=1e5:1.5e5;
spikeCountND=NaN(13,trials);
spikeCountG=NaN(13,trials);
for i=1:numel(ISteps)
    for j=1:trials
        [pksND locsND]=findpeaks(tracesNoDrugs(stepIdx,i,j),'minPeakHeight',-20,'minpeakDistance',200);
        [pksG locsG]=findpeaks(tracesGabazine(stepIdx,i,j),'minPeakHeight',-20,'minpeakDistance',200);
        spikeCountND(i,j)=numel(locsND)*2;
         spikeCountG(i,j)=numel(locsG)*2;
    end
       
end

figure;
plot(ISteps,mean(spikeCountND,2),'--','markersize',20)
hold on
plot(ISteps,mean(spikeCountG,2),'--','markersize',20)
legend('No Drugs','Gabazine','Location','northwest')
 plot(ISteps,spikeCountND, '.','markersize',20,'MarkeredgeColor',[ 0    0.4470    0.7410]);
 plot(ISteps,spikeCountG, '.','markersize',20,'MarkeredgeColor',[ 0.8500    0.3250    0.0980]);
 title([path2(end-11:end-10) '.' path2(end-8:end-7) '.' path2(end-5:end-4) '  Cell ' path2(end-2:end-1) ' IO curve No Drugs vs Gabazine (5�M)'])
ylabel('Firing rate (spikes/s)');
xlabel('Injected Current (pA)');


